home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 235 / Issue 235 - September 2007 - DPCS0907DVD.ISO / Extras / NetObjects Fusion / NOF10.exe / data1.cab / FSI / lib / nof / util / logging / Logger.js < prev    next >
Encoding:
Text File  |  2007-04-11  |  7.0 KB  |  197 lines

  1. /****i* SOURCE_FILE/INFO
  2.   *
  3.   * NAME
  4.   *  Logger.js
  5.   *
  6.   * USAGE
  7.   *  Part of Netobjects JavaScript Library.
  8.   *
  9.   * COPYRIGHT
  10.   *  Copyright ⌐ 2000-2005 Website Pros, Inc.
  11.   *  All Rights Reserved.
  12.   *
  13.   *  This is an unpublished work protected by Website Pros, Inc.
  14.   *  as a trade secret, and is not to be used or disclosed except as
  15.   *  expressly provided in a written license agreement executed by
  16.   *  you and Website Pros, Inc.
  17.   *
  18.   *      <copyright@websitepros.com>
  19.   *
  20.   * NOTES
  21.   *  JavaScript code.
  22.   *
  23.   *****/
  24.  
  25. if (!IS_isModuleInitialized("IS.NOF.UTIL.LOGGING.Logger"))
  26. {   
  27.   /****h* NOF_JavaScript_Library/NOF.UTIL.LOGGING.Logger
  28.     *
  29.     * NAME
  30.     *  NOF.UTIL.LOGGING.Logger
  31.     *
  32.     * DESCRIPTION
  33.     *  
  34.     * The <code>Logger</code> class provides the methods to trace (and notify the 
  35.     * LOGGING system of) the events occured in an application module, 
  36.     * like info, warning, severe or log. 
  37.     * It offers the possibility to manage a list of handlers (predefined, 
  38.     * like <code>Handler</code>, <code>ConsoleHandler</code> or user defined ones).
  39.     * Each handler in the list will receive a log record (as parameter of <code>Handler.publish</code> method)
  40.     * It can have also a resource bundle from which the LOGGING mechanism would extract localized messages.
  41.     *
  42.     ****/
  43.   
  44.   /**
  45.   * constructor (should not be called from outside the LOGGING module).
  46.   * @param id - Logger identification string
  47.   * @param bundle - localized resource
  48.   **/
  49.   function LOGGING_Logger( /*string*/ id, bundle ) {
  50.     this.__proto__ = LOGGING_Logger.prototype;
  51.     
  52.     this.ID = id; 
  53.     this.resourceBundle = bundle;
  54.     
  55.     this.level = 0;
  56.     this.handlers = new Array();
  57.   }
  58.   {
  59.     var member = LOGGING_Logger.prototype;    
  60.     member.CLASS_NAME        = "LOGGING.Logger";
  61.     
  62.     var method = LOGGING_Logger.prototype;                    
  63.     /**
  64.     * Get this Logger id.
  65.     **/
  66.     method.getLoggerName = function () { return this.ID; }
  67.     
  68.     /**
  69.     * Set the minumum level from which the records passed through this Logger should be processed
  70.     * @param level one of the values defined by LOGGING.Level interface
  71.     **/        
  72.     method.setLevel = function (/*int*/ level) { this.level = level; }
  73.     /**
  74.     * Get the minimum log level from which the records passed through this Logger will be processed.
  75.     * @return level associated with this record.
  76.     **/        
  77.     method.getLevel = function () { return this.level;}
  78.     
  79.     /**
  80.     * Determine if a record with a given level will be processed by this Logger.
  81.     * 
  82.     * @param level
  83.     * @return true if the level parameter is less or equal than the Logger's level set.
  84.     **/
  85.     method.isLoggable = function (/*int*/ level) { return (this.level >= level); }
  86.  
  87.     /**
  88.     * Append a new Handler at the list of the handlers of this Logger.
  89.     * 
  90.     * @param hnd the handler instance
  91.     **/        
  92.     method.addHandler = function (/*NOF.UTIL.LOGGING.Handler*/ hnd) { 
  93.       if (this.handlers.containsItem(hnd) < 0) { 
  94.         this.handlers[this.handlers.length] = hnd; 
  95.       } 
  96.     }
  97.     /**
  98.     * Remove a specified Handler from the handlers list of this Logger.
  99.     * 
  100.     * @param hnd the handler instance
  101.     **/                
  102.     method.removeHandler = function (/*NOF.UTIL.LOGGING.Handler*/ hnd) { 
  103.       this.handlers.removeItem(this.handlers.containsItem(hnd)); 
  104.     }
  105.     /**
  106.     * Get the Logger's list of handlers 
  107.     * 
  108.     * @return the list (Array) of handlers of this Logger
  109.     **/
  110.     method.getHandlers = function () { return this.handlers; }
  111.     
  112.     /**
  113.     * Logs a record, if loggable.
  114.     * @param logRecord
  115.     **/
  116.     /* protected */ method.doLog = function (/*NOF.UTIL.LOGGING.LogRecord*/ logRecord) {    
  117.       if (this.isLoggable(logRecord.getLevel())) { 
  118.         for (var i=0; i < this.handlers.length; i++) {                     
  119.           this.handlers[i].publish(logRecord); 
  120.         } 
  121.       }             
  122.     }
  123.     
  124.     /**
  125.     * Convenience method to log important messages (of whose notification is critical). 
  126.     * Equivalent of calling: log(LOGGING.Level.SEVERE, sourceClass, sourceMethod, message, params);
  127.     * @param message
  128.     * @param sourceClass (optional)
  129.     * @param sourceMethod (optional)
  130.     * @param params    parameters used for formatting the message (optional)    
  131.     **/
  132.     method.severe = function (message, sourceClass, sourceMethod, params) {    
  133.       this.log(LOGGING.Level.SEVERE, sourceClass, sourceMethod, message, params); 
  134.     }
  135.     
  136.     /**
  137.     * Convenience method to log warning messages (of whose notification is important). 
  138.     * Equivalent of calling: log(LOGGING.Level.WARNING, sourceClass, sourceMethod, message, params);
  139.     * @param message
  140.     * @param sourceClass (optional)
  141.     * @param sourceMethod (optional)
  142.     * @param params    parameters used for formatting the message (optional)    
  143.     **/        
  144.     method.warning = function (message, sourceClass, sourceMethod, params) {    
  145.       this.log(LOGGING.Level.WARNING, sourceClass, sourceMethod, message, params);  
  146.     }
  147.     
  148.     /**
  149.     * Convenience method to log state information or to trace the activity of the module.
  150.     * (to be used for medium level importance messages)
  151.     * Equivalent of calling: log(LOGGING.Level.INFO, sourceClass, sourceMethod, message, params);
  152.     * @param message
  153.     * @param sourceClass (optional)
  154.     * @param sourceMethod (optional)
  155.     * @param params    parameters used for formatting the message (optional)    
  156.     **/        
  157.     method.info = function (message, sourceClass, sourceMethod, params) {    
  158.       this.log(LOGGING.Level.INFO, sourceClass, sourceMethod, message, params); 
  159.     }   
  160.     
  161.     /**
  162.     * The method to be used if no other convenience method is provided for your needs.
  163.     * Based on the parameters, it creates a LogRecord object which (if stated as qualified for LOGGING
  164.     * by the isLoggable() method) will be passed to all the Handler objects of this Logger to be published.
  165.     * 
  166.     * @param level 
  167.     * @param message
  168.     * @param sourceClass (optional)
  169.     * @param sourceMethod (optional)
  170.     * @param params    parameters used for formatting the message (optional)    
  171.     **/                
  172.     method.log = function (level, sourceClass, sourceMethod, msg, /*Object[]*/ params) {
  173.       var lr = new LOGGING.LogRecord(level, msg);
  174.       if (typeof(sourceClass) != "undefined") lr.setSourceClassName(sourceClass);
  175.       if (typeof(sourceMethod) != "undefined") lr.setSourceMethodName(sourceMethod);
  176.       if (typeof(params) != "undefined") lr.setParameters(params);
  177.       lr.setResourceBundle(this.resourceBundle);
  178.       this.doLog(lr);
  179.     }
  180.     
  181.     /**
  182.     * Set the localization resource bundle of this Logger.
  183.     *
  184.     * @param bundle localization bundle
  185.     */
  186.     method.setResourceBundle = function (bundle) { this.resourceBundle = bundle; }
  187.     /**
  188.     * Get the localization resource bundle of this Logger.
  189.     *
  190.     * @return localization bundle
  191.     **/        
  192.     method.getResourceBundle = function () { return this.resourceBundle; }
  193.     
  194.   }
  195.   
  196.   LOGGING.__proto__.Logger = LOGGING_Logger;
  197. }